package com.ma.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

    //初始化
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("----初始化----");

    }
    /*chain
    1.过滤器中的所有代码，在过滤待定请求的时候都会执行
    2.必须要让过滤器继续通行，
    chain.doFile
    **/
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html");

        System.out.println("CharacterEncodingFilter执行前----");
        filterChain.doFilter(servletRequest,servletResponse);//使请求继续走
        System.out.println("CharacterEncodingFilter执行后----");
    }
    //销毁
    public void destroy() {
        System.out.println("----销毁！！！!");

    }
}
